448C - Painting Fence - CodeForces Solution


divide and conquer dp greedy *1900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int N = 5e3 + 7;
int a[N];
int func(int l, int r) {
	int len = r - l;
	if (len <= 0) {
		return 0;
	}
	if (len == 1) {
		if (a[l] == 0) return 0;
		else return 1;
	}
	int k = 1e9 + 7;
	int p = 0;
	for (int i = l; i < r; i++) {
		if (k > a[i]) {
			p = i;
			k = a[i];
		}
	}
	int j = k;
	for (int i = l; i < r; i++) {
	     a[i] -= j;
	     if (a[i] < 0) a[i] = 0;
	}
	return min(len, (func(l, p) + func(p + 1, r) + k));
	
}
int main () {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int h = func(0, n);
	cout << h;
}


Comments

Submit
0 Comments
More Questions

922A - Cloning Toys
817A - Treasure Hunt
1136B - Nastya Is Playing Computer Games
1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays
424C - Magic Formulas
1730C - Minimum Notation
1730B - Meeting on the Line
1730A - Planets
302B - Eugeny and Play List
1730D - Prefixes and Suffixes
1515C - Phoenix and Towers
998A - Balloons
1734F - Zeros and Ones
1144B - Parity Alternated Deletions
92B - Binary Number
1144C - Two Shuffled Sequences
1154B - Make Them Equal
1272B - Snow Walking Robot
522B - Photo to Remember
608B - Hamming Distance Sum
1408F - Two Different
274B - Zero Tree
1726H - Mainak and the Bleeding Polygon
722A - Broken Clock